package com.wanpinghui.supplier.business.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.user.adapter.BusinessAreaAdapter;
import com.wanpinghui.wphlibrary.model.user.bean.AreaBean;

import java.util.List;

/**
 * Created by tg596 on 2018/1/27.
 */

public class WhoCanSeeAdapter extends BaseExpandableListAdapter {

    private final List<AreaBean> areaBeanList;
    private Context context;

    public WhoCanSeeAdapter(List<AreaBean> areaBeanList,Context context) {
        this.areaBeanList = areaBeanList;
        this.context = context;
    }

    public List<AreaBean> getData(){
        return areaBeanList;
    };

    @Override
    public int getGroupCount() {
        return areaBeanList == null ? 0 : areaBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (areaBeanList.get(groupPosition).getChildren() != null) {
            return areaBeanList.get(groupPosition).getChildren().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return areaBeanList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return areaBeanList.get(groupPosition).getChildren();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            groupViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            groupViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);
            groupViewHolder.tv_count = (TextView)convertView.findViewById(R.id.tv_count);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final AreaBean areaBeen = areaBeanList.get(groupPosition);
        groupViewHolder.textViewContent.setText(areaBeen.getName());

        if (areaBeen.getChildren() != null && areaBeen.getChildren().size()>0){
            groupViewHolder.tv_count.setVisibility(View.GONE);
            groupViewHolder.imageViewSelect.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.my_icon_));
            if (isExpanded){
                groupViewHolder.imageViewSelect.animate().setDuration(500).rotation(180).start();
            }else {
                groupViewHolder.imageViewSelect.animate().setDuration(500).rotation(0).start();
            }
            groupViewHolder.imageViewSelect.setOnClickListener(null);
        }else {

            groupViewHolder.imageViewSelect.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.selector_common_checkbox_blue));

            if (areaBeen.isSelect()) {
                if (groupPosition == 2){  //全国工程商
                    groupViewHolder.tv_count.setVisibility(View.VISIBLE);
                    int count = areaBeen.getEngineer_quantity()-areaBeen.getFirend_quantity();
                    groupViewHolder.tv_count.setText("共"+areaBeen.getEngineer_quantity()+"人，需消耗"+count+"汇币");
                }else {
                    groupViewHolder.tv_count.setVisibility(View.GONE);
                }

                groupViewHolder.imageViewSelect.setSelected(true);

            } else {
                groupViewHolder.tv_count.setVisibility(View.GONE);
                groupViewHolder.imageViewSelect.setSelected(false);

            }
            groupViewHolder.imageViewSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!areaBeen.isSelect()){
                        setSelect(groupPosition);
                    }

                }
            });

        }


        return convertView;
    }

    public void setSelect(int index) {
        if (getGroupCount() > index) {
            for (int i = 0; i < areaBeanList.size(); i++) {
                if (i == index) {
                    areaBeanList.get(i).setSelect(true);
                }else {
                    areaBeanList.get(i).setSelect(false);
                    if (areaBeanList.get(i).getChildren() != null) {
                        for (AreaBean bean : areaBeanList.get(i).getChildren()) {
                            bean.setSelect(false);
                        }
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final GroupViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_area, parent, false);
            childViewHolder = new GroupViewHolder();
            childViewHolder.textViewContent = (TextView) convertView.findViewById(R.id.tv_content);
            childViewHolder.imageViewSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            childViewHolder.ll_area = (RelativeLayout) convertView.findViewById(R.id.ll_area);
            childViewHolder.tv_count = (TextView)convertView.findViewById(R.id.tv_count);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (GroupViewHolder) convertView.getTag();
        }
        final AreaBean customerAreachildbean = areaBeanList.get(groupPosition).getChildren().get(childPosition);
        childViewHolder.ll_area.setBackgroundColor(ContextCompat.getColor(context, R.color.colorBackgroundF7));
        childViewHolder.textViewContent.setText(customerAreachildbean.getName());
        if (customerAreachildbean.isSelect()) {
            //  childViewHolder.textViewContent.setSelected(true);
            childViewHolder.tv_count.setVisibility(View.VISIBLE);
            childViewHolder.imageViewSelect.setSelected(true);
            int count = customerAreachildbean.getEngineer_quantity()-customerAreachildbean.getFirend_quantity();
            childViewHolder.tv_count.setText("共"+customerAreachildbean.getEngineer_quantity()+"人，需消耗"+count+"汇币");
        } else {
            childViewHolder.tv_count.setVisibility(View.GONE);
            //    childViewHolder.textViewContent.setSelected(false);
            childViewHolder.imageViewSelect.setSelected(false);
        }
        childViewHolder.ll_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (customerAreachildbean.isSelect()) {
                    childViewHolder.tv_count.setVisibility(View.GONE);
                    customerAreachildbean.setSelect(false);
                } else {
                    setSelect(groupPosition);
                    childViewHolder.tv_count.setVisibility(View.VISIBLE);
                    int count = customerAreachildbean.getEngineer_quantity()-customerAreachildbean.getFirend_quantity();
                    customerAreachildbean.setBid_point(count);
                    childViewHolder.tv_count.setText("共"+customerAreachildbean.getEngineer_quantity()+"人，需消耗"+count+"汇币");
                    customerAreachildbean.setSelect(true);
                }
                notifyDataSetChanged();

            }
        });


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private class GroupViewHolder {
        TextView textViewContent;
        ImageView imageViewSelect;
        RelativeLayout ll_area;
        ImageView iv_jiantou;
        TextView tv_all;
        TextView tv_count;
    }

}
